﻿Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common


''' <summary>
''' 将数据输出到Auto Cad中
''' </summary>
''' <remarks></remarks>

Public Class ClassAcadOut

    Private acad As AcadApplication
    Public Sub ACADOut(ByVal CadFileName As String, ByVal ringList As List(Of IClassTypeRing), Optional ByVal lineList As List(Of TypeLine) = Nothing, Optional ByVal nodeList As List(Of TypeNode) = Nothing)
        On Error Resume Next
        Dim acad As AcadApplication = getAcadApplication()
        Dim doc As AcadDocument = acad.Documents.Add()
        doc.SaveAs(CadFileName)


        ACADOutRing(CadFileName, ringList)
        ACADOutLine(CadFileName, lineList)
        ACADOutNode(CadFileName, nodeList)

    End Sub


    ''' <summary>
    ''' 将环数据输出到Acad 中
    ''' </summary>
    ''' <param name="CadFileName">输出的CAD文件名称</param>
    ''' <param name="ringList">环数据</param>
    ''' <remarks></remarks>
    Public Sub ACADOutRing(ByVal CadFileName As String, ByVal ringList As List(Of IClassTypeRing))
        On Error Resume Next
        Dim acad As AcadApplication = getAcadApplication()
        Dim doc As AcadDocument = acad.ActiveDocument
        '置当前层
        Dim layer As AcadLayer = doc.Layers.Add("闭合环")
        doc.ActiveLayer = layer

        ''对环进行输出
        For Each tmpRing As IClassTypeRing In ringList
            Dim ringPoly As AcadPolyline
            Dim point((tmpRing.getNodeCount + 1) * 3 - 1) As Double
            Dim i = 0
            For Each tmpNode As TypeNode In tmpRing.getNodeList
                point(i) = tmpNode.latitude
                point(i + 1) = tmpNode.longitude
                point(i + 2) = tmpNode.altitude
                i = i + 3
            Next
            '进行闭合
            point(i) = point(0)
            point(i + 1) = point(1)
            point(i + 2) = point(2)


            ringPoly = doc.ModelSpace.AddPolyline(point)
        Next
        doc.Save()
        acad.Visible = True
        acad.ZoomAll()
        acad = Nothing


    End Sub
    ''' <summary>
    ''' 将点数据输出到Acad中
    ''' </summary>
    ''' <param name="CadFileName"></param>
    ''' <param name="nodeList"></param>
    ''' <remarks></remarks>
    Public Sub ACADOutNode(ByVal CadFileName As String, ByVal nodeList As List(Of TypeNode))
        '    On Error Resume Next
        Dim acad As AcadApplication = getAcadApplication()
        Dim doc As AcadDocument = acad.ActiveDocument

        '置当前层
        Dim layer As AcadLayer = doc.Layers.Add("测点")
        doc.ActiveLayer = layer

        ''对环进行输出
        For Each tmpNode As TypeNode In nodeList
            Dim ringPoly As AcadPoint
            Dim point(3) As Double
            point(0) = tmpNode.latitude
            point(1) = tmpNode.longitude
            point(2) = tmpNode.altitude
            ringPoly = doc.ModelSpace.AddPoint(point)
        Next
        doc.Save()
        acad.Visible = True
        acad.ZoomAll()
        acad = Nothing
    End Sub
    ''' <summary>
    ''' 将边数据输出到Acad中
    ''' </summary>
    ''' <param name="CadFileName"></param>
    ''' <param name="lineList"></param>
    ''' <remarks></remarks>
    Public Sub ACADOutLine(ByVal CadFileName As String, ByVal lineList As List(Of TypeLine))
        ' On Error Resume Next
        Dim acad As AcadApplication = getAcadApplication()
        Dim doc As AcadDocument = acad.ActiveDocument
        '置当前层
        Dim layer As AcadLayer = doc.Layers.Add("测边")
        doc.ActiveLayer = layer

        ''对环进行输出
        For Each tmpLine As TypeLine In lineList
            If tmpLine.getLineType = ClassConsManager.LineTypeEnum.SIMPLE_LINE Then
                Dim ringPoly As AcadLine
                Dim point1(3), point2(3) As Double
                Dim tmpNode As TypeNode
                Dim i = 0
                tmpNode = tmpLine.startNode
                point1(0) = tmpNode.latitude
                point1(1) = tmpNode.longitude
                point1(2) = tmpNode.altitude

                tmpNode = tmpLine.endNode
                point2(0) = tmpNode.latitude
                point2(1) = tmpNode.longitude
                point2(2) = tmpNode.altitude
                ringPoly = doc.ModelSpace.AddLine(point1, point2)
            End If
        Next
        doc.Save()
        acad.Visible = True
        acad.ZoomAll()
        acad = Nothing
    End Sub

    ''' <summary>
    ''' 生成acadapplication对象
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>


    Private Function getAcadApplication() As AcadApplication
        If acad Is Nothing Then
            Try
                acad = GetObject(, "AutoCAD.Application")
            Catch ex As Exception

                acad = CreateObject("AutoCAD.Application")
            End Try
            Return acad

        Else
            Return acad

        End If

    End Function

End Class
